﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;

namespace ZWZ.Web.WebAdmin
{
    public partial class ug : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserGroupList.Columns = new List<UCtrs.ListColumn>()
                {
                    new UCtrs.ListColumn(){ Caption="名称", FieldName="Name", Width="auto", Style="text-align:left;"},
                    new UCtrs.ListColumn(){ Caption="类型", FieldName="Type", Width="60px"},
                };
                BindData();
            }
        }
        protected void UserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string id = e.CommandArgument.ToString();
                var oldmodel = DBProvider.DB.Ctx.User_Group.Where(i => i.ID == id).FirstOrDefault();
                if (oldmodel != null)
                {
                    DBProvider.DB.Ctx.User_Group.DeleteObject(oldmodel);
                    DBProvider.DB.Ctx.SaveChanges();
                }
                BindData();
            }
        }
        protected void UserList_ItemDataBond(object source, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                //判断权限
                if ((e.Item.DataItem as ModelBase.User_Group).Type == ZWZ.Enums.User.GroupType.系统.ToString())
                {
                    (e.Item.FindControl("BtnUpdate") as ImageButton).Visible=false;
                    (e.Item.FindControl("BtnDelete") as ImageButton).Visible=false;
                }
            }
        }
        protected void UserList_PageChanging(object src, PageChangingEventArgs e)
        {
            UserGroupList.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }
        protected void BindData()
        {
            UserGroupList.RecordCount = DBProvider.DB.Ctx.User_Group.Count();
            UserGroupList.DataSource = DBProvider.DB.Ctx.User_Group
                .OrderBy(i => i.Name)
                .Skip((UserGroupList.CurrentPageIndex - 1) * UserGroupList.PageSize)
                .Take(UserGroupList.PageSize);
            UserGroupList.DataBind();
        }
    }
}